package com.ykq.dao;

import com.ykq.entity.User;

public class UserDao extends BaseDao{
    public User selectByNameAndPassword(String username, String password) {
        User user = null;
        try {
            String sql = "select * from tbl_user where username=? and password=?";
            getConn();
            ps=conn.prepareStatement(sql);
            ps.setObject(1, username);
            ps.setObject(2, password);
            rs=ps.executeQuery();
            while(rs.next()){
                //从数据库中获取指定列的值
                int uid = rs.getInt("id");
                String username1 = rs.getString("username");
                int age = rs.getInt("age");
                String password1 = rs.getString("password");
                String realname= rs.getString("realname");
                //封装到实体类中
                user = new User();
                user.setId(uid);
                user.setUsername(username1);
                user.setAge(age);
                user.setPassword(password1);
                user.setRealname(realname);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeAll();
        }
        return user;
    }
}
